package org.jeecg.modules.system.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.system.base.controller.BaseController;
import org.jeecg.modules.system.entity.SysPosition;
import org.jeecg.modules.system.entity.SysUserPosition;
import org.jeecg.modules.system.service.ISysUserPositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;

/**
 * @Description:
 */
@Slf4j
@Api(tags = "用户职务表")
@RestController
@RequestMapping("/sys/userPosition")
public class SysUserPositionController extends BaseController<SysUserPosition,ISysUserPositionService> {

    @Autowired
    private ISysUserPositionService sysUserPositionService;

    /**
     * 分页列表查询
     *
     * @param sysPosition
     * @return
     */
    @AutoLog(value = "用户职务表-分页列表查询")
    @ApiOperation(value = "用户职务表-分页列表查询", notes = "用户职务表-分页列表查询")
    @GetMapping(value = "/list")
    @PermissionData(pageComponent = "system/DeptRoleUserList")
    public Result<IPage<SysUserPosition>> list(SysUserPosition sysPosition) {
        IPage<SysUserPosition> page = sysUserPositionService.findPage(sysPosition);
        return Result.OK(page);
    }

    /**
     * 添加
     *
     * @return
     */
    @AutoLog(value = "用户职务表-添加")
    @ApiOperation(value = "用户职务表-添加", notes = "用户职务表-添加")
    @PostMapping(value = "/add")
    public Result<SysPosition> add(@RequestBody SysUserPosition sysUserPosition) {
        Result<SysPosition> result = new Result<SysPosition>();
        try {
            sysUserPositionService.save(sysUserPosition);
            result.success("添加成功！");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.error500("操作失败");
        }
        return result;
    }

    /**
     * 编辑
     *
     * @return
     */
    @AutoLog(value = "用户职务表-编辑")
    @ApiOperation(value = "用户职务表-编辑", notes = "用户职务表-编辑")
    @PutMapping(value = "/edit")
    public Result<SysPosition> edit(@RequestBody SysUserPosition sysUserPosition) {
        Result<SysPosition> result = new Result<SysPosition>();
        boolean ok = sysUserPositionService.updateById(sysUserPosition);
        if (ok) {
            result.success("修改成功!");
        }
        else {
            result.error500("修改失败");
        }

        return result;
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "用户职务表-通过id删除")
    @ApiOperation(value = "用户职务表-通过id删除", notes = "用户职务表-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        try {
            sysUserPositionService.removeById(id);
        } catch (Exception e) {
            log.error("删除失败", e.getMessage());
            return Result.error("删除失败!");
        }
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "用户职务表-批量删除")
    @ApiOperation(value = "用户职务表-批量删除", notes = "用户职务表-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        Result result = new Result();
        if (ids == null || "".equals(ids.trim())) {
            result.error500("参数不识别！");
        } else {
            this.sysUserPositionService.removeByIds(Arrays.asList(ids.split(",")));
            result.success("删除成功!");
        }
        return result;
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "职务表-通过id查询")
    @ApiOperation(value = "职务表-通过id查询", notes = "职务表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<SysUserPosition> queryById(@RequestParam(name = "id", required = true) String id) {
        Result<SysUserPosition> result = new Result<SysUserPosition>();
        SysUserPosition entity = sysUserPositionService.getById(id);
        if (entity == null) {
            result.error500("未找到对应实体");
        } else {
            result.setResult(entity);
            result.setSuccess(true);
        }
        return result;
    }


}
